<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfcomponent name="PermissionService" output="false" extends="_PermissionService">
	
    <cfset variables.objectInstance = structNew() />
    <cfset variables.allPerms = structNew() />
	

	<cffunction name="init" access="public" output="false" returntype="PermissionService">
		<cfargument name="PermissionDAO" type="PermissionDAO" required="true" />
		<cfargument name="PermissionGateway" type="PermissionGateway" required="true" />
        <cfargument name="ObjTypeService" type="ayc.cfc.object.ObjTypeService" required="yes" />
        
        <cfset var aPerms = "" />
        <cfset var iii = "" />
        <cfset var nextObj = "" />

		<cfset super.init(arguments.PermissionDAO, arguments.PermissionGateway) />
        <!--- load permissions for all db objects --->
        
        <cfset aPerms = arguments.PermissionGateway.getByAttributes(orderby="objectTypeId, seq") />
        <cfloop from="1" to="#arrayLen(aPerms)#" index="iii">
        	<cfset nextObj = arguments.ObjTypeService.getObjType(aPerms[iii].getObjectTypeId()) />
            <cfif NOT structKeyExists(variables.allPerms, "#nextObj.getObjectTypeName()#")>
            	<cfset variables.allPerms["#nextObj.getObjectTypeName()#"] = "" />
            </cfif>
            <cfset variables.allPerms["#nextObj.getObjectTypeName()#"] = listAppend(variables.allPerms["#nextObj.getObjectTypeName()#"], aPerms[iii].getPermissionName()) />
        </cfloop>
        

		<cfreturn this/>
	</cffunction>
    
    <cffunction name="getGateway" access="public" output="false" returntype="PermissionGateway">
    	<cfreturn variables.PermissionGateway />
    </cffunction>
    
    <cffunction name="getDAO" access="public" output="false" returntype="PermissionDAO">
    	<cfreturn variables.PermissionDAO />
    </cffunction>
    
    <cffunction name="getAllPerms" access="public" output="false" returntype="struct">
    	<cfreturn variables.allPerms />
    </cffunction>
    
    <cffunction name="checkPerm" access="public" output="false" returntype="boolean">
    	<cfargument name="objectType" type="string" required="yes" />
    	<cfargument name="userPermission" type="string" required="yes" />
        <cfargument name="permLevel" type="string" required="yes" />
        
        <cfif NOT structKeyExists(variables.allPerms, "#arguments.objectType#")>
        	<cfreturn false />
        <cfelseif listFindNoCase(variables.allPerms["#arguments.objectType#"], arguments.permLevel) EQ 0>
        	<cfreturn false />
        <cfelse>
        	<cfif listFindNoCase(variables.allPerms["#arguments.objectType#"], arguments.userPermission) GTE listFindNoCase(variables.allPerms["#arguments.objectType#"], arguments.permLevel)>
            	<cfreturn true />
            <cfelse>
            	<cfreturn false />
            </cfif>
        </cfif>
    </cffunction>
    
</cfcomponent>


